/**
 * Created by Administrator on 2016/10/9.
 */
import { notification, Modal } from 'antd';
import {browserHistory} from 'react-router';
import * as axios from 'axios';
axios.defaults.baseURL = window.GLOBAL.baseURL;
axios.defaults.headers.post['Content-Type'] = window.GLOBAL.contentType;

//美妆网站 前端注册
export function signUp(obj, paramData) {
    obj.setState({loading: true});
    axios.post('/webAPI/unAuth/signUp', paramData)
    .then(function (response) {
        obj.setState({loading: false});
        if(response.data.code == 1){
            sessionStorage.setItem("username", paramData.username);
            sessionStorage.setItem('userAuth', response.data.data);
            axios.defaults.headers.common['userAuth'] = response.data.data;
            Modal.success({
              title: '提示：',
              content: '账号注册成功。',
              onOk() {
                browserHistory.push("/");
              }
            });
        }else{
          notification.error({
              placement: 'bottomRight',
              message: '提示：',
              description: '对不起, 你注册的账号已存在.',
              style: {
                borderLeft: '4px solid rgb(240, 70, 52)'
              }
          });
        }
    }).catch(function(err){
        obj.setState({loading: false});
        notification.error({
            placement: 'bottomRight',
            message: '提示：',
            description: '对不起，网络请求错误。',
            style: {
              borderLeft: '4px solid rgb(240, 70, 52)'
            }
        });
    })
}
//美妆网站 前端登录
export function login(obj, paramData) {
    obj.setState({loading: true});
    axios.post('/webAPI/unAuth/login', paramData)
    .then(function (response) {
        obj.setState({loading: false});
        if(response.data.code == 1){
            sessionStorage.setItem('userAuth', response.data.data.token);
            sessionStorage.setItem('username', paramData.email || paramData.username);
            axios.defaults.headers.common['userAuth'] = response.data.data.token;
            browserHistory.push("/");
        }else{
          notification.error({
              placement: 'bottomRight',
              message: '提示：',
              description: '对不起，账户和密码错误。',
              style: {
                borderLeft: '4px solid rgb(240, 70, 52)'
              }
          });
        }
    }).catch(function(err){
        obj.setState({loading: false});
        notification.error({
            placement: 'bottomRight',
            message: '提示：',
            description: '对不起，网络请求错误。',
            style: {
              borderLeft: '4px solid rgb(240, 70, 52)'
            }
        });
    })
}
//查询个人信息
export function findProfileById(obj) {
    if(sessionStorage.getItem("userAuth") != null){
        axios.defaults.headers.common['userAuth'] = sessionStorage.getItem('userAuth');
    }
    axios.post('/webAPI/setting/findProfileById')
    .then(function (response) {
        if(response.data.code != 0){
          if(response.data.data.photoPic == undefined){
            obj.setState({myProfile: response.data.data, imageUrl: "/src/styles/images/default.png"});
          }else {
            obj.setState({myProfile: response.data.data, imageUrl: "http://tinsley.oss-cn-shenzhen.aliyuncs.com/" + response.data.data.photoPic});
          }
        }
    })
}
//修改个人信息
export function updateProfile(obj, paramData) {
    if(sessionStorage.getItem("userAuth") != null){
        axios.defaults.headers.common['userAuth'] = sessionStorage.getItem('userAuth');
    }
    obj.setState({ loading: true, tipUpdate: "提交中，请稍候..." });
    axios.post('/webAPI/setting/updateProfile', paramData)
    .then(function (response) {
        if(response.data.code != 0){
          Modal.success({
            title: '提示：',
            content: '你的 个人信息 修改成功。',
            onOk() {
              obj.setState({ loading: false });
            }
          });
        }else{
          obj.setState({ loading: false });
          notification.error({
              placement: 'bottomRight',
              message: '提示：',
              description: '你的 个人信息 修改失败。',
              style: {
                borderLeft: '4px solid rgb(240, 70, 52)'
              }
          });
        }
    }).catch(function(err){
        obj.setState({ loading: false });
        notification.error({
            placement: 'bottomRight',
            message: '提示：',
            description: '对不起，网络请求错误。',
            style: {
              borderLeft: '4px solid rgb(240, 70, 52)'
            }
        });
    })
}
//修改密码
export function updateUserPassword(obj, paramData) {
    if(sessionStorage.getItem("userAuth") != null){
        axios.defaults.headers.common['userAuth'] = sessionStorage.getItem('userAuth');
    }
    axios.post('/webAPI/setting/updateUserPassword', paramData)
    .then(function (response) {
        if(response.data.data == true){
          sessionStorage.removeItem("userAuth");
          sessionStorage.removeItem("username");
          browserHistory.push("/login");

          notification.success({
              placement: 'bottomRight',
              message: '提示：',
              description: '修改密码成功。',
              style: {
                borderLeft: '4px solid rgb(23, 168, 84)'
              }
          });
        }else if(response.data.data == false){
          obj.setState({ visible: false });
          notification.error({
              placement: 'bottomRight',
              message: '提示：',
              description: '对不起，旧密码错误。',
              style: {
                borderLeft: '4px solid rgb(240, 70, 52)'
              }
          });
        }
    })
}
//查找登录人员的详情信息
export function findUserProfile(obj) {
    if(sessionStorage.getItem("userAuth") != null){
        axios.defaults.headers.common['userAuth'] = sessionStorage.getItem('userAuth');
    }
    axios.post('/webAPI/settings/findUserProfile', null)
    .then(function (response) {
        if(response.data.code == 1){
            if(response.data.data != undefined){
                obj.setState({serverData: response.data.data})
                if(response.data.data.roles != undefined && response.data.data.roles.length >= 1){
                    axios.post('/webAPI/role/findRoleDetail/' + response.data.data.roles[0], null)
                    .then(function (response) {
                        if(response.data.code == 1){
                          var strPermissiones = "";
                          for (var i = 0; i < response.data.data.permissiones.length; i++) {
                              strPermissiones += response.data.data.permissiones[i].pName + "、";
                          }
                          strPermissiones = strPermissiones.substr(0, strPermissiones.length-1);
                          obj.setState({
                            roles: response.data.data.role.roleName,
                            strPermissiones: strPermissiones
                          });
                        }
                    })
                }
            }
        }
    })
}
//修改个人信息数据
export function updateUserProfile(obj, paramData) {
    if(sessionStorage.getItem("userAuth") != null){
        axios.defaults.headers.common['userAuth'] = sessionStorage.getItem('userAuth');
    }
    axios.post('/webAPI/settings/updateUserProfile', paramData)
    .then(function (response) {
        if(response.data.code != 0){
          obj.setState({ visiblePersonal: false,visibleAlert: false, modifyLoading: false });
          obj.props.form.resetFields();
          notification.success({
              placement: 'bottomRight',
              message: '提示：',
              description: '修改信息成功。',
              style: {
                borderLeft: '4px solid rgb(23, 168, 84)'
              }
          });
        }else{
          obj.setState({ visiblePersonal: false,visibleAlert: false, modifyLoading: false });
          notification.error({
              placement: 'bottomRight',
              message: '提示：',
              description: '对不起，信息修改错误。',
              style: {
                borderLeft: '4px solid rgb(240, 70, 52)'
              }
          });
        }
    }).catch(function(err){
        obj.setState({ visiblePersonal: false,visibleAlert: false, modifyLoading: false });
        notification.error({
            placement: 'bottomRight',
            message: '提示：',
            description: '对不起，网络请求错误。',
            style: {
              borderLeft: '4px solid rgb(240, 70, 52)'
            }
        });
    })
}
 // /webAPI  / settings/updateParentPortrait
